<template>
  <el-card>
  <div slot="header" >
    <my-breadcrumb>评论管理</my-breadcrumb>
  </div>
  <!-- 表格 -->
      <el-table :data="comments">
      <el-table-column
        prop="title"
        label="文章标题">
      </el-table-column>
      <el-table-column
        prop="total_comment_count"
        label="总评论数">
      </el-table-column>
      <el-table-column
        prop="fans_comment_count"
        label="粉丝评论数">
      </el-table-column>
      <el-table-column
        prop=""
        label="评论状态">
        <!--自定义的列模板-->
        <template slot-scope="scope">
            <!-- {{scope.row.comment_status}} -->
            <el-tag v-if="scope.row.comment_status">正常</el-tag>
            <el-tag v-else type="warning">关闭</el-tag>
          </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
            <!-- {{scope.row.comment_status}} -->
            <el-button size="mini" v-if="scope.row.comment_status" type="danger"
            @click="hSwitch(scope.row)">关闭</el-button>
            <el-button size="mini" v-else type="primary"
            @click="hSwitch(scope.row)">打开</el-button>
          </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="hPageChange"
      :total="total_count">
</el-pagination>
</el-card>
</template>

<script>
export default {
  name: 'Comment',
  data () {
    return {
      total_count: 0, // 总条数
      page: 1, // 页码
      comments: [] // 评论数据
    }
  },
  created () {
    this.loadComments()
  },
  methods: {
    buildParams () {
      const params = {
        page: this.page,
        response_type: 'comment'
      }
      return params
    },
    async loadComments () {
      const paramObj = this.buildParams()
      try {
        const res = await this.$axios({
          method: 'get',
          url: '/mp/v1_0/articles',
          params: paramObj
        })
        console.log(res)
        this.comments = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        console.log(err)
      }
    },
    hPageChange (curPage) {
      // 更新页面
      this.page = curPage
      // 重发请求
      this.loadComments()
    },
    // 打开,或者关闭评论
    async hSwitch (row) {
      console.log(row)
      try {
        const res = await this.$axios({
          method: 'put',
          url: '/mp/v1_0/comments/status',
          params: {
            article_id: row.id
          },
          data: {
            allow_comment: !row.comment_status
          }
        })
        this.$message.success('操作成功')
        console.log(res)
        // 更新数据
        row.comment_status = !row.comment_status
      } catch (err) {
        console.log(err)
        this.$message.error('操作失败')
      }
    }
  }
}
</script>

<style>

</style>
